package com.gxa.modules.jwt;

import com.alibaba.fastjson.JSONObject;
import com.gxa.common.utils.JwtResult;
import com.gxa.common.utils.JwtUtils;
import com.gxa.modules.pojo.User;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: microloans_system
 * @description: 解析token
 * @author: LC
 * @create: 2022-01-05 19:31
 */
public class ParseTokenUtils {

    /**
     * 获取当前用户信息
     * @param request
     * @return
     */
    public static User getCurrentUser(HttpServletRequest request){
        String token=request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            return null;
        }
        JwtResult result = JwtUtils.validateJwt(token);
        String subjectStr=result.getClaims().getSubject();
        User user= JSONObject.parseObject(subjectStr,User.class);
        return user;
    }
}